package com.gzsxy.esjy.service.core.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.gzsxy.esjy.service.core.sign.entity.ArraignSign;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;


@Slf4j
public class SignFlowUtil {




    public static void setSignFileData(ArraignSign arraignSign, String nodeCode, boolean cover) {
        String nodeSignFiles = arraignSign.getNodeSignFiles();
        if (StringUtils.isBlank(nodeSignFiles)) {
            Map<String, String> temp = new HashMap<>(8);
            temp.put(nodeCode, arraignSign.getLatestFile());
            arraignSign.setNodeSignFiles(JSON.toJSONString(temp));
        } else {
            JSONObject jsonObject = JSON.parseObject(nodeSignFiles);
            if (cover){
                jsonObject.put(nodeCode, arraignSign.getLatestFile());
                arraignSign.setNodeSignFiles(jsonObject.toJSONString());
            }else {
                if (!jsonObject.containsKey(nodeCode)) {
                    jsonObject.put(nodeCode, arraignSign.getLatestFile());
                    arraignSign.setNodeSignFiles(jsonObject.toJSONString());
                }
            }


        }
    }

    public static void setSignFileData(ArraignSign arraignSign, String nodeCode) {
        String nodeSignFiles = arraignSign.getNodeSignFiles();
        if (StringUtils.isBlank(nodeSignFiles)) {
            Map<String, String> temp = new HashMap<>(8);
            temp.put(nodeCode, arraignSign.getLatestFile());
            arraignSign.setNodeSignFiles(JSON.toJSONString(temp));
        } else {
            JSONObject jsonObject = JSON.parseObject(nodeSignFiles);
            if (!jsonObject.containsKey(nodeCode)) {
                jsonObject.put(nodeCode, arraignSign.getLatestFile());
                arraignSign.setNodeSignFiles(jsonObject.toJSONString());
            }
        }
    }






    public static String getSignFile(ArraignSign arraignSign, String nodeCode) {
        String nodeSignFiles = arraignSign.getNodeSignFiles();
        if (StringUtils.isNotBlank(nodeSignFiles)) {
            JSONObject jsonObject = JSON.parseObject(nodeSignFiles);
            if (jsonObject.containsKey(nodeCode)) {
                return (String) jsonObject.get(nodeCode);
            }
        }

        return null;
    }
}
